﻿using Microsoft.AspNetCore.Http;
using Syspetro.Core.ErrorException;
using Syspetro.Deploy.Jwt;
using System;
using System.Linq;
using System.Security.Claims;

namespace Syspetro.Deploy.Managers
{
    public class AuthorizationManager : IAuthorizationManager
    {
        private readonly bool isLogin = true;
        private IUserAuth user;
        /// <summary>
        /// 请求上下文访问器
        /// </summary>
        private readonly IHttpContextAccessor _httpContextAccessor;
        /// <summary>
        /// 构造函数
        /// </summary> 
        /// <param name="httpContextAccessor"></param>
        /// <param name="userRepository"></param>
        public AuthorizationManager(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            if (!_httpContextAccessor.HttpContext.User.Claims.Any())
            {
                isLogin = false;
            }
        }
        public IUserAuth GetUser<T>() where T : IUserAuth, new()
        {
            if (user == null)
            {
                if (!isLogin)
                    return null;
                user = new T
                {
                    UserId = GetUserId,
                    IsAdmin = IsAdmin,
                    Account = GetTypeValue("Account"),
                    RoleName = GetTypeValue(ClaimTypes.Role),
                    UserName = GetTypeValue(ClaimTypes.Name)
                };
            }
            return user;
        }
        public IUserAuth GetUser()
        {
            if (!isLogin)
                return null;
            return GetUser<BaseUserAuth>();
        }
        public Guid GetUserId
        {
            get
            {
                if (!isLogin)
                    return Guid.Empty;
                return Guid.Parse(GetTypeValue("UserId"));
            }
        }
        public string IP
        {
            get
            {
                string ip = this._httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                if (ip == "172.0.0.1" || ip == "::1")
                    ip = _httpContextAccessor.HttpContext.Request.Headers["X-Real-IP"].FirstOrDefault();
                return ip;
            }
        }
        public string GetPhone
        {
            get
            {
                if (!isLogin)
                    return "";
                return GetTypeValue("Phone");
            }
        }
        public string GetEmil
        {
            get
            {
                if (!isLogin)
                    return "";
                return GetTypeValue("Emil");
            }
        }
        public string GetUserName
        {
            get
            {
                return GetTypeValue(ClaimTypes.Name);
            }
        }
        public bool IsAdmin
        {
            get
            {
                if (!isLogin)
                    return false;
                return bool.Parse(_httpContextAccessor.HttpContext.User.FindFirstValue("IsAdmin"));
            }
        }
        private string GetTypeValue(string str)
        {
            return _httpContextAccessor.HttpContext.User.FindFirstValue(str);
        }
    }
}
